<?php

namespace app\comapi\controller;

use app\common\controller\Frontend;
use app\common\model\banxi\word\WordResources as WordResourcesModel;
use app\common\service\ConfigService;
use app\common\service\UploadService;
use ba\Exception;
use think\exception\ValidateException;

/**
 * 配置
 */
class WordResources extends Frontend
{
    protected array $noNeedLogin = ['*'];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 查询配置JSON格式
     * @return void
     */
    public function getVocal(): void
    {
        $param = $this->request->param(['text', 'resourcesType']);
        try {
            $wordResourcesModel = new WordResourcesModel();
            $vocal = $wordResourcesModel->getVocal($param['text'], $param['resourcesType']);
            // 清理输出缓冲区，确保不会干扰音频流
            header('Content-Type: audio/mpeg');
            echo $vocal;
            flush();

        } catch (\Exception|ValidateException $e) {
            $this->fail($e->getMessage());
        }
    }


    /**
     * 查询-favicon
     */
    public function getFavicon()
    {
        $ConfigService = new ConfigService();
        $data = $ConfigService->getSysConfigValue('logo');
        $uploadService = new UploadService();
        $bytes = $uploadService->downloadBody($data);
        try {
            header('Content-Type: image/*');
            echo $bytes;
            flush();
        } catch (Exception $e) {
            $this->fail($e->getMessage());
        }
    }

    /**
     * 查询-剪纸盒提示音
     * @return void
     */
    public function getDecoupageBoxAudio(): void
    {
        $ConfigService = new ConfigService();
        $data = $ConfigService->getSysConfigValue('decoupage_box_audio');
        $this->ok('操作成功', $data);
    }

    /**
     * 查询-剪纸盒错误提示音
     * @return void
     */
    public function getDecoupageBoxErrorAudio(): void
    {
        $ConfigService = new ConfigService();
        $data = $ConfigService->getSysConfigValue('decoupage_box_error_audio');
        $this->ok('操作成功', $data);
    }

}